package com.whatever;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import android.content.Context;
import android.graphics.*;
import android.view.View.*;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View implements OnTouchListener {

	protected ArrayList<ColorBall> colorBall; //Create a dynamic list of colorBall objects
	private ArrayList<ColorBall> ballsToDelete;
	private Iterator<ColorBall> iterator; //Iterator constructed from ColorBall objects
	private int width; //Width of device screen
	private int height; //Height of device screen
	public int ballsDestroyed;
	private Random rand = new Random(); //Global Random
	private Paint paint; //Paint constant
	private Context cxt; //Context constant

	public DrawView(Context cxt) {
		super(cxt);
		this.cxt = cxt; //Store imported context to local

		ballsDestroyed = 0;

		//Get device dimensions
		WindowManager windowManager = (WindowManager)cxt.getSystemService(Context.WINDOW_SERVICE); 
		Display display = windowManager.getDefaultDisplay();
		this.width = display.getWidth(); //Store device width locally
		this.height = (display.getHeight() - 150); //Store device height locally

		colorBall = new ArrayList<ColorBall>(); //Initialize the dynamic list of ColorBalls
		ballsToDelete = new ArrayList<ColorBall>();

		setFocusableInTouchMode(true); //Set the touch sensible area to this
		setOnTouchListener(this); //Set the onTouchListener to this view
	}

	public synchronized void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);

		paint = new Paint();
		paint.setColor(0xff0000ff); //Set the color of "paint" to blue(Placeholder for paint)
		paint.setAntiAlias(true);

		ColorBall ball; //Declare a single ball
		iterator = colorBall.iterator(); //Define an iterator from the LinkedList "colorBall"
		while(iterator.hasNext()) { //While there are more values in "iterator"...
			try {
				ball = (ColorBall) iterator.next(); //Define the single ball to the current list entry processed by iterator
				ball.onMove(ball.getPosX(), ball.getPosY(), ball); //Process ball movement...
				//if(ball.getLifeLeft() <= 0) {
				//	ballsToDelete.add(ball);
				//}
				paint.setColor(ball.getColor()); //Set paint to the color of the ball
				canvas.drawCircle(ball.getPosX(), ball.getPosY(), ball.getRadius(), paint); //Draw the ball
			} catch (Exception e) {
				//System.out.println(e);
			}
		}
		/*for(int i = 0; i < ballsToDelete.size(); i++) {
			colorBall.remove(ballsToDelete.get(i)); //Remove tapped balls
			ballsDestroyed++;
			NewGameActivity.calculateScore(ballsDestroyed);
			NewGameActivity.updateScore();
			System.out.println("Balls Destroyed: " + ballsDestroyed);
		}*/
		//ballsToDelete = new ArrayList<ColorBall>();
		
		notifyAll();
		invalidate();
	}

	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction(); //Store the action from the event
		ColorBall ball; //Declare a single ball
		iterator = colorBall.iterator(); //Define an iterator from the LinkedList "colorBall"
		switch(action) { //Based on what type of action occurred...
		case MotionEvent.ACTION_DOWN: //If the action was a tap...
			while(iterator.hasNext()) { //While there are more entries in the iterator...
				ball = (ColorBall) iterator.next(); //Define the single ball as the stored ball in iterator
				if(((int) Math.sqrt(Math.pow((ball.getPosX() - event.getX()), 2) + (Math.pow((ball.getPosY() - event.getY()), 2)))) <= ball.getRadius() + 10) { //If the tap was within the circle's radius of the center...
					ballsToDelete.add(ball); //Add the tapped ball to a linked list to store in order to delete later
				}
			}
			break;
		case MotionEvent.ACTION_MOVE: //If the action was a swipe, do nothing
		case MotionEvent.ACTION_UP: //If the action was a release, do nothing
		default:
		}

		for(int i = 0; i < ballsToDelete.size(); i++) { //For every ball tapped...
			colorBall.remove(ballsToDelete.get(i)); //Remove tapped balls
			ballsDestroyed++;
			NewGameActivity.calculateScore(ballsDestroyed);
			NewGameActivity.updateScore();
			System.out.println("Balls Destroyed: " + ballsDestroyed);
		}
		ballsToDelete = new ArrayList<ColorBall>();; //Clear the list of balls to delete, so that the next tick it won't try to delete the same (now nonexistant) balls again
		return true;
	}

	public void createBall(int number) {
		for(int i = 0; i < number; i++) {
			colorBall.add(new ColorBall(cxt, (rand.nextInt(9999) % width), (rand.nextInt(9999) % height))); //Create a colorBall in a random location on the screen
		}
	}
	
	public int getBallsDestroyed(){
		return ballsDestroyed;
	}
}

